package com.xyz.mycompose

import android.annotation.SuppressLint
import android.content.Intent
import android.os.Bundle
import android.widget.Toast
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.interaction.MutableInteractionSource
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.RowScope
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxHeight
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.wrapContentHeight
import androidx.compose.foundation.layout.wrapContentSize
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.foundation.verticalScroll
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Add
import androidx.compose.material.icons.filled.Home
import androidx.compose.material.icons.filled.Menu
import androidx.compose.material.icons.filled.MoreVert
import androidx.compose.material.icons.filled.Person
import androidx.compose.material.icons.filled.Share
import androidx.compose.material3.CenterAlignedTopAppBar
import androidx.compose.material3.DismissibleDrawerSheet
import androidx.compose.material3.DrawerState
import androidx.compose.material3.DrawerValue
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.Icon
import androidx.compose.material3.IconButton
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.ModalNavigationDrawer
import androidx.compose.material3.NavigationBar
import androidx.compose.material3.Scaffold
import androidx.compose.material3.Surface
import androidx.compose.material3.Text
import androidx.compose.material3.TopAppBarDefaults
import androidx.compose.material3.rememberDrawerState
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.vector.ImageVector
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import com.xyz.mycompose.ui.theme.MyApplication1Theme
import kotlinx.coroutines.async

class DrawerActivity : ComponentActivity() {
    // 创建并记住 NavController 用于导航
//    val navController = rememberNavController()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent {
            MyApplication1Theme {
                Surface(
                    modifier = Modifier.fillMaxSize(),
                    color = MaterialTheme.colorScheme.background
                ) {
                    DrawerPreview()
                }
            }
        }
    }

    @SuppressLint("UnusedMaterial3ScaffoldPaddingParameter")
    @OptIn(ExperimentalMaterial3Api::class)
    @Preview(showBackground = true)
    @Composable
    fun DrawerPreview() {
        MyApplication1Theme {
            val context = LocalContext.current
            val drawerState = rememberDrawerState(initialValue = DrawerValue.Closed)
            ModalNavigationDrawer(
                modifier = Modifier.fillMaxWidth(),
                drawerState = drawerState,
                drawerContent = {
                    DismissibleDrawerSheet() {
                        Text(text = "asdasda")
                        Text(text = "asdasda")
                        Text(text = "asdasda")
                    }
                    /*Column(
                        modifier = Modifier
                            .fillMaxHeight()
                            .background(color = Color.White)
                            .width(300.dp)
                            .clickable(false) {}
                    ) {
                        Text(text = "asdasda")
                        Text(text = "asdasda")
                        Text(text = "asdasda")
                    }*/
                }) {
                Scaffold(
                    topBar = { MyTopBarDrawer(drawerState) },
                    bottomBar = { MyBottomAppBar() },
                    content = { paddingValues ->
                        Box(
                            modifier = Modifier.padding(paddingValues)
                        ) {
                            Column(
                                modifier = Modifier
                                    .fillMaxSize()
                                    .background(color = Color.Red)
                            ) {
                                Row(
                                    modifier = Modifier
                                        .height(50.dp)
                                ) {
                                    Text(text = "账号安全")
                                }
                            }
                        }
                    }
                )
            }
        }
    }


    @OptIn(ExperimentalMaterial3Api::class)
    @Composable
    fun MyTopBar() {
        val context = LocalContext.current
        CenterAlignedTopAppBar(
            modifier = Modifier.height(45.dp),
            colors = TopAppBarDefaults.mediumTopAppBarColors(containerColor = Color.LightGray),
            title = {
                Box(
                    modifier = Modifier
                        .fillMaxHeight()
                        .wrapContentHeight()
                ) {
                    Text(
                        fontSize = 16.sp, textAlign = TextAlign.Center,
                        text = "标题", color = Color.Black
                    )
                }
            },
            navigationIcon = {
                IconButton(onClick = {

                }) {
                    Icon(imageVector = Icons.Default.Menu, contentDescription = "holle")
                }
            },
            actions = {
                IconButton(onClick = {
                    Toast.makeText(context, "分享", Toast.LENGTH_SHORT).show()
                }) {
                    Icon(imageVector = Icons.Default.Share, contentDescription = "holle")
                }
                IconButton(onClick = {
                }) {
                    Icon(imageVector = Icons.Default.MoreVert, contentDescription = "holle")
                }
            }
        )
    }

    @OptIn(ExperimentalMaterial3Api::class)
    @Composable
    fun MyTopBarDrawer(drawerState: DrawerState) {
        val context = LocalContext.current
        val scope = rememberCoroutineScope()
        CenterAlignedTopAppBar(
            modifier = Modifier.height(45.dp),
            colors = TopAppBarDefaults.mediumTopAppBarColors(containerColor = Color.LightGray),
            title = {
                Box(
                    modifier = Modifier
                        .fillMaxHeight()
                        .wrapContentHeight()
                ) {
                    Text(
                        fontSize = 16.sp, textAlign = TextAlign.Center,
                        text = "标题", color = Color.Black
                    )
                }
            },
            navigationIcon = {
                IconButton(onClick = {
                    scope.async { drawerState.open() }
                }) {
                    Icon(imageVector = Icons.Default.Menu, contentDescription = "holle")
                }
            },
            actions = {
                IconButton(onClick = {
                    Toast.makeText(context, "分享", Toast.LENGTH_SHORT).show()
                }) {
                    Icon(imageVector = Icons.Default.Share, contentDescription = "holle")
                }
                IconButton(onClick = {
                }) {
                    Icon(imageVector = Icons.Default.MoreVert, contentDescription = "holle")
                }
            }
        )
    }

    @Composable
    fun RowScope.CustomNavigationBarItem(
        icon: ImageVector,
        label: String,
        selected: Boolean,
        onClick: () -> Unit,
        selectedIconColor: Color,
        unselectedIconColor: Color,
        selectedTextColor: Color,
        unselectedTextColor: Color
    ) {
        Column(
            modifier = Modifier
                .weight(1f, true)
                .clickable(
                    interactionSource = remember { MutableInteractionSource() },
                    indication = null,
                    onClick = onClick
                )
                .fillMaxHeight()
                .wrapContentSize(),
            horizontalAlignment = Alignment.CenterHorizontally
        ) {
            Icon(
                imageVector = icon, contentDescription = null,
                tint = if (selected) selectedIconColor else unselectedIconColor
            )
            Text(
                text = label, fontSize = 12.sp,
                color = if (selected) selectedTextColor else unselectedTextColor
            )
        }
    }

    @Composable
    fun MyBottomAppBar() {
        var selectedItem by remember { mutableStateOf(0) }
        val items = listOf("首页", "添加", "我的")
        val icons = listOf(Icons.Default.Home, Icons.Default.Add, Icons.Default.Person)
        NavigationBar(
            modifier = Modifier.height(55.dp),
            containerColor = Color.LightGray,
        ) {
            items.forEachIndexed { index, item ->
                CustomNavigationBarItem(
                    icon = icons[index],
                    label = item,
                    selected = selectedItem == index,
                    onClick = { selectedItem = index },
                    selectedIconColor = Color.Blue,
                    unselectedIconColor = Color.Black,
                    selectedTextColor = Color.Blue,
                    unselectedTextColor = Color.Black
                )
            }
        }
        /*NavigationBar(
            containerColor = Color.LightGray,
        ) {
            items.forEachIndexed { index, item ->
                NavigationBarItem(
                    icon = {
                        Icon(
                            imageVector = icons[index], contentDescription = null
                        )
                    },
                    label = {
                        Text(
                            text = item, fontSize = 10.sp
                        )
                    },
                    selected = selectedItem == index,
                    onClick = { selectedItem = index },
                    colors = NavigationBarItemDefaults.colors(
                        selectedIconColor = Color.Blue,
                        unselectedIconColor = Color.Black,
                        selectedTextColor = Color.Blue,
                        unselectedTextColor = Color.Black,
                        indicatorColor = Color.LightGray
                    )
                )
            }
        }*/
        /*BottomAppBar(
            modifier = Modifier.height(55.dp),
            containerColor = Color.LightGray,
            contentColor = Color.Blue
        ) {
            IconButton(modifier = Modifier.weight(1f, true), onClick = {
            }) {
                Column(horizontalAlignment = Alignment.CenterHorizontally) {
                    Icon(imageVector = Icons.Default.Home, contentDescription = "holle")
                    Text(text = "首页", fontSize = 12.sp)
                }
            }
            IconButton(modifier = Modifier.weight(1f, true), onClick = {
            }) {
                Column(horizontalAlignment = Alignment.CenterHorizontally) {
                    Icon(imageVector = Icons.Default.Person, contentDescription = "holle")
                    Text(text = "我的", fontSize = 12.sp)
                }
            }
        }*/
    }
}
